package com.pan.server.mapper;

import com.pan.pojo.vo.OrderVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {

    /**
     *
     * @param userId
     * @return
     */
    List<OrderVO> queryOrders(Long userId);

    /**
     *
     * @param map
     */
    void addOrderDetail(Map<String, Object> map);

    /**
     *
     * @param map
     */
    void addOrder(Map<String, Object> map);

    /**
     *
     * @param map
     */
    @Update("update `orderr` set status = #{status} where order_id = #{orderId}")
    void updateOrder(Map<String, Object> map);

    /**
     *
     * @param orderId
     */
    @Delete("delete from `orderr` where order_id = #{orderId}")
    void deleteOrder(Integer orderId);

    /**
     *
     * @param orderId
     * @return
     */
    @Select("select * from `orderr` where order_id = #{orderId}")
    OrderVO queryOrderById(Integer orderId);

    /**
     *
     * @param orderId
     */
    @Update("update book set stock = stock - (select quantity from order_detail where order_id = #{orderId}) where book_id = (select book_id from order_detail where order_id = #{orderId})")
    void updateBookStock(Integer orderId);

    /**
     *
     * @param orderId
     * @return
     */
    @Select("select quantity from order_detail where order_id=#{orderId}")
    int queryOrderQuantity(Integer orderId);

    /**
     *
     * @param map
     * @return
     */
    @Select("select orderr.order_id from orderr where user_id=#{userId} and order_time=#{now}")
    Long searchOrders(Map<String, Object> map);

    /**
     *
     * @param orderId
     */
    @Delete("delete from order_detail where order_id=#{orderId}")
    void deleteOrderDetail(Integer orderId);

    /**
     *
     * @param id
     */
    @Delete("delete from `orderr` where user_id=#{id}")
    void deleteOrderByUserId(Integer id);

    /**
     *
     * @param id
     * @return
     */
    @Select("select order_id from `orderr` where user_id=#{id}")
    List<Integer> queryOrdersByUserId(Integer id);

    /**
     *
     * @param id
     * @return
     */
    @Select("select order_id from order_detail where book_id=#{id}")
    List<Integer> queryOrdersDetailByBookId(Integer id);
}